#!/bin/sh

set -eu

OPT_ACCEPT=0
OPT_TEST=0

for ARG; do
        case "$ARG" in
        -a | --accept)
                OPT_ACCEPT=1
                ;;
        -t | --test)
                OPT_TEST=1
                ;;
        esac
done

EXIT=0

for TEST in tests/*.mkc; do
        echo "snaptest: $TEST"

        ./makec "$TEST"

        TEST_OK=1

        if [ $OPT_ACCEPT -eq 1 ]; then
                cp "${TEST%.mkc}".c "$TEST".expect.c
        elif ! diff "$TEST".expect.c "${TEST%.mkc}".c; then
                TEST_OK=0
        fi

        if [ $OPT_TEST -eq 1 ] && [ $TEST_OK -eq 1 ]; then
                ./makec -r "$TEST"
        fi

        if [ $TEST_OK -eq 1 ]; then
                rm "${TEST%.mkc}".c
        else
                EXIT=1
        fi
done

exit $EXIT
